/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.sdcache.distributed;

import com.sdcache.distributed.cache.CacheManager;
import java.lang.Thread;



/**
 *
 * @author chaitanya
 */
public class SessionTester 
{
    class Writer implements Runnable
    {
        int i=0;
        public Writer(int i) {
            this.i=i;
        }

        public void run()
        {
            long t =System.currentTimeMillis();
            int j=0;
            while(true)
            {
                i++;
                Session session= new Session(i);
                sessionManager.put(i, session);
                if(i%1000==0)
                {
                    j++;
                    System.out.println("WRITER   "+(System.currentTimeMillis()-t)/j);
                    System.out.println(i);
                    try
                    {
                            Thread.sleep(100);
                    } catch (Exception exception)
                    {
                    }
                }
            }
        }

    }
    public CacheManager sessionManager;

    public SessionTester()
    {
        sessionManager = new CacheManager(1111, "127.0.0.1", 1112);
        //sessionManager = new CacheManager(1112, "127.0.0.1", 1111);
        //test();
        try
        {
            Thread.sleep(10000);
        } catch (Exception exception)
        {
        }
        new Thread(new Writer(1)).start();
        try
        {
            Thread.sleep(7000);
        } catch (Exception exception)
        {
        }

        new Thread(new Deleter()).start();
       // new Thread(new Getter()).start();
    }

    public void test()
    {
        System.out.println("-------------------------------");
        //System.out.println(sessionManager.storeSession(1, new Session(1)));
        System.out.println("--------------------"+sessionManager.get(1));
        //System.out.println(sessionManager.deleteSession(1));
    }
    

    class Getter implements Runnable
    {

        int i=0;
        public void run()
        {
            long t =System.currentTimeMillis();
            int j=0;
            while(true)
            {
                i=i+1;
                sessionManager.get(i);
                if(i%1000==0)
                {
                    j++;
                    System.out.println("GETTER   "+(System.currentTimeMillis()-t)/j);
                    System.out.println( sessionManager.get(i));
                    try
                    {
                            Thread.sleep(100);
                    } catch (Exception exception)
                    {
                    }

                }
            }
        }
        
    }
    
    class Deleter implements Runnable
    {
        int i=1;
        public void run()
        {
            long t =System.currentTimeMillis();
            int j=0;
            while(true)
            {
                i=i+2;
                sessionManager.remove(j);
                if(i%1000==0)
                {
                    j++;
                    System.out.println("DELETER   "+(System.currentTimeMillis()-t)/j);
                    try
                    {
                            Thread.sleep(100);
                    } catch (Exception exception)
                    {
                    }
                }
            }
        }
        
    }
     public static void main(String[] args)
    {
        new SessionTester();
    }

}
